import { defineStore } from 'pinia';
import { getUserInfo } from '@/api/user';

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: null
  }),
  
  getters: {
    isLogin: (state) => !!state.token,
    isAuth: (state) => state.userInfo?.isAuth || false
  },
  
  actions: {
    setToken(token) {
      this.token = token;
      localStorage.setItem('token', token);
    },
    
    async fetchUserInfo() {
      try {
        const data = await getUserInfo();
        this.userInfo = data;
        return data;
      } catch (error) {
        console.error('获取用户信息失败:', error);
        return null;
      }
    },
    
    logout() {
      this.token = '';
      this.userInfo = null;
      localStorage.removeItem('token');
    }
  }
}); 